package com.itheima.mobilesafe.home;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import com.itheima.mobilesafe.R;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends Activity {
private TextView tv_version; //应用版本号
    //新加的
    private Timer mTimer = new Timer();
    private CountdownCircleProgressBar mCircleProgressBar;
    /**
     * 开屏时长固定为5000ms
     */
    public final static int OPEN_SCREEN_TIME = 5000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        init();
//新加
        mCircleProgressBar = (CountdownCircleProgressBar) findViewById(R.id.skipBtn);
        mCircleProgressBar.setTimeMillis(OPEN_SCREEN_TIME);
        //mCircleProgressBar 播放动画
        mCircleProgressBar.play();
        //mCircleProgressBar 跳过按钮点击事件
        mCircleProgressBar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startMainActivity();
            }
        });

        //使用计时器Task来等待5秒跳转
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                startMainActivity();
            }
        };
        mTimer.schedule(timerTask,OPEN_SCREEN_TIME);
    }
    //实现显示版本号功能
    private void init() {
        tv_version = findViewById(R.id.tv_version);
        try {
            //获取程序包信息
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            tv_version.setText("当前版本：V" + info.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            tv_version.setText("当前版本：V");
        }
    }
    /**
     * 跳转到MainActivity，同时finish SplashActivity
     */
    private void startMainActivity(){
        startActivity(new Intent(SplashActivity.this,HomeActivity.class));
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注意：splashActivity销毁时应该停止线程，防止内存泄漏！！！
        mCircleProgressBar.isRunning = false;
        // 注意：splashActivity销毁时应该停止mTimer，防止内存泄漏！！！
        mTimer.cancel();
    }
}



